<?php
class LZ_Photo_Category
{
	var $table = 'photo_category';
	
	var $id = 'category_id';
	var $name = 'name';
	var $description = 'description';
	var $order_id = 'order_id';	
	var $category_image = 'category_image';
	
	function LZ_Photo_Category()
	{
		$this->table = LZ_MYSQL_PREFIX.$this->table;
	}

	function add( $data = array())
	{
		global $db;
		$sql = make_insert_sql($this->table,$data);
		$re = $db->query($sql);
		return $re;
	}
	function update($id, $data = array())
	{
		global $db;
		$sql = make_update_sql($this->table,$data, array('category_id' => $id)); 
		return ($db->query($sql));
	}
	function delete($id)
	{
		global $db;
		$sql = "DELETE FROM `$this->table` WHERE `$this->id` = '$id'";
		return $db->query($sql);
	}
	
	function get_one($id)
	{
		global $db;
		$sql = "SELECT * FROM `{$this->table}` WHERE `{$this->id}` = '$id' LIMIT 1";
		return $db->get_one($sql);
	}

	/*
	*Input: array
	*  search: like %{search}%
	*  order: order by {order}
	*  desc? 'DESC': 'ASC'
	*  from total: LIMIT {from},{total}
	*  total: LIMIT {total}
	*/
	function get_list($data=array())
	{
		global $db;
		$sql = "SELECT * FROM `{$this->table}` WHERE 1=1 ";
		if ($data['search'])
		{
			$sql.=" AND (`$this->name` LIKE '%".$data['search']."%' OR `$this->description` LIKE '%".$data['search']."%' )";
		}

		$sql.= " ORDER BY ";
		
		if ($data['order'])
		{
			$sql.= $data['order'];
			$sql.= ($data['desc'])?' DESC':' ASC';
		}
		else
		{
			$sql.= $this->order_id.' DESC';
		}

		

		if ($data['from'] && $data['total'])
		{
			$sql.=" LIMIT ".$data['from'].",".$data['total'];
		}
		else if ($data['total'])
		{
			$sql.=" LIMIT ".$data['total'];
		}
		$sql.= ';';
		return $db->get_all($sql);
	}
}
?>